package com.neutech.shop.controller;

import com.neutech.shop.entity.Category;
import com.neutech.shop.entity.ConditionVO;
import com.neutech.shop.entity.Product;
import com.neutech.shop.service.CategoryService;
import com.neutech.shop.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Neuedu-远哥
 * @version 1.0
 * @date 2021/9/29 10:47
 */
@Controller
@RequestMapping("/back/product")
public class ProductController {
    @Resource
    private ProductService service;
    @Resource
    private CategoryService categoryService;
    @RequestMapping("/list")
    public String findAll(ModelMap map){
        List<Product> products = service.findAll();
        List<Category> categories = categoryService.findThirdGrade();
        map.addAttribute("products",products);
        map.addAttribute("categories",categories);

        return "product_list.jsp";
    }

    @RequestMapping("/insert")
    public String insert(Product product, @RequestParam("file") CommonsMultipartFile file,HttpServletRequest request){
        service.insert(product,file,request);
        return "redirect:list";
    }
    @RequestMapping("/selectByKeywords")
    public String selectByKeywords(String keywords,ModelMap map){
        List<Product> products = service.selectByKeywords(keywords);
System.out.println(products);
        map.addAttribute("products",products);
        return "forward:product_list.jsp";
    }
    @RequestMapping("/searchProductComplex")
    public String searchProductComplex(ConditionVO conditionVO,ModelMap map){
        List<Product> products = service.searchProductComplex(conditionVO);
        map.addAttribute("products", products);
        return "forward:product_complex_search_complete.jsp";
    }
    @ResponseBody
    @RequestMapping("/selectThreeCategories")
    public List<Category> selectThreeCategories(){
        return categoryService.findThirdGrade();
    }
}
